/**
 * 纸张设置相关的可组合函数
 * 
 * 该模块封装了与纸张尺寸和打印设置相关的功能
 */

import { reactive, ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { handleError } from '../utils/errorHandling';
import { adjustPaperSettingsByType } from '../config/templateFieldsConfig';

/**
 * 创建纸张设置功能
 * @param {Object} options 配置选项
 * @param {Function} options.updateRulerDimensions 更新刻度尺尺寸的函数
 * @returns {Object} 纸张设置相关的方法和属性
 */
export default function usePaperSettings(options = {}) {
  const { updateRulerDimensions } = options;

  // 标记用户是否手动修改过纸张设置
  const userCustomized = ref(false);

  // 纸张设置
  const paperSettings = reactive({
    paperWidth: 148, // 纸张宽度，单位mm
    paperHeight: 105, // 纸张高度，单位mm
    printDirection: '打印机默认设置',
    printBorderType: '实际纸张边缘'
  });

  // 已应用的纸张设置（用于实际渲染）
  const appliedPaperSettings = reactive({
    paperWidth: 148,
    paperHeight: 105
  });

  // 每毫米像素数 - 用于单位转换
  const pxPerMm = ref(3.78); // 默认值，将在初始化时动态计算

  // 计算画布内容尺寸
  const canvasContentWidth = computed(() => appliedPaperSettings.paperWidth);
  const canvasContentHeight = computed(() => appliedPaperSettings.paperHeight);

  /**
   * 设置每毫米像素数
   * @param {number} value 每毫米像素数
   */
  const setPxPerMm = (value) => {
    pxPerMm.value = value;
    console.log(`设置像素比例: ${value} px/mm`);
  };

  /**
   * 应用纸张设置
   * @param {Object} settings 要应用的设置
   */
  const applyPaperSettings = (settings) => {
    try {
      console.log('应用纸张设置:', settings);
      
      // 更新纸张设置
      if (settings.width) {
        paperSettings.paperWidth = settings.width;
        userCustomized.value = true; // 标记用户已手动设置
      }
      if (settings.height) {
        paperSettings.paperHeight = settings.height;
        userCustomized.value = true; // 标记用户已手动设置
      }
      if (settings.printDirection) paperSettings.printDirection = settings.printDirection;
      if (settings.printBorderType) paperSettings.printBorderType = settings.printBorderType;
      
      // 更新应用的设置
      appliedPaperSettings.paperWidth = paperSettings.paperWidth;
      appliedPaperSettings.paperHeight = paperSettings.paperHeight;
      
      // 如果提供了更新刻度尺的函数，则调用它
      if (updateRulerDimensions) {
        updateRulerDimensions();
      }
      
      ElMessage.success('纸张设置已更新');
      console.log('更新后的纸张尺寸:', paperSettings.paperWidth, 'x', paperSettings.paperHeight);
    } catch (error) {
      handleError(error, '应用纸张设置失败');
    }
  };

  /**
   * 根据打印类型调整纸张设置
   * @param {string} printType 打印类型
   * @param {boolean} force 是否强制覆盖用户设置
   */
  const adjustSettingsByType = (printType, force = false) => {
    try {
      // 如果用户已手动设置且不是强制覆盖，则不调整设置
      if (userCustomized.value && !force) {
        console.log('用户已手动设置纸张尺寸，将不会根据打印类型自动调整');
        return;
      }
      
      // 调整纸张设置
      adjustPaperSettingsByType(printType, paperSettings);
      
      // 应用设置变更
      applyPaperSettings(paperSettings);
      
      // 如果是强制覆盖，重置标记
      if (force) userCustomized.value = false;
    } catch (error) {
      handleError(error, '调整纸张设置失败');
    }
  };


  return {
    paperSettings,
    appliedPaperSettings,
    pxPerMm,
    canvasContentWidth,
    canvasContentHeight,
    setPxPerMm,
    applyPaperSettings,
    adjustSettingsByType,
    userCustomized
  };
}
